package com.tech.connect.zhaofuwu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ksy.common.image.ImageLoader;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.ProxyLayout;
import com.ksy.common.view.PullToRefreshLayout;
import com.tech.connect.R;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.QuznZiHttp;
import com.tech.connect.fragment.BaseFragment;
import com.tech.connect.model.ItemQuznZi;
import com.tech.connect.model.PagerModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class QuanZiVideoFragment extends BaseFragment {

    private BaseAdapter<ItemQuznZi, BaseHolder> adapter;
    private RecyclerView recycler;
    private List<ItemQuznZi> allList = new ArrayList<>();
    private int page = 1;
    private int pageSize = 15;
    private ProxyLayout<RecyclerView> proxyLayout;

    @Override
    protected boolean isShowHeadBar() {
        return true;
    }

    @Override
    protected boolean isFullStateBar() {
        return true;
    }

    @Override
    public View onCreateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.layout_recycler, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getHeadBar().setLeftIcon(R.drawable.icon_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (activity != null) {
                    activity.onBackPressed();
                }
            }
        }).setTitle("服务圈").setRightText("发布", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, PublishQuanZiActivity.class);
                intent.putExtra("isVideo", true);
                jump2Activity(intent, 996);

            }
        });
        initView(view);
        loadData();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 996 && resultCode == Activity.RESULT_OK) {
            loadData();
        }
    }

    private void initView(View view) {
        recycler = view.findViewById(R.id.recycler);
        recycler.setLayoutManager(new GridLayoutManager(activity, 2) {
            @Override
            public boolean canScrollVertically() {
                return true;
            }
        });
        proxyLayout = new ProxyLayout<>(activity, recycler);
        proxyLayout.setCanRefresh(true);
        proxyLayout.setDragListener(new PullToRefreshLayout.DragListener() {
            @Override
            public void drag2Upper(PullToRefreshLayout pullToRefreshLayout) {
                ++page;
                loadData();
            }

            @Override
            public void drag2Downer(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                loadData();
            }
        });
    }


    private void initAdapter(List<ItemQuznZi> list) {
        if (list != null) {
            allList.addAll(list);
        }
        if (adapter == null) {
            adapter = new BaseAdapter<ItemQuznZi, BaseHolder>(R.layout.item_layout_quanzi_video, allList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    final ItemQuznZi item = allList.get(position);
                    ImageView ivImage = helper.getView(R.id.ivCover);
                    ImageView ivHead = helper.getView(R.id.ivHead);
                    TextView tvContent = helper.getView(R.id.tvContent);
                    TextView tvName = helper.getView(R.id.tvName);
                    TextView tvPraise = helper.getView(R.id.tvPraise);
                    ImageLoader.getInstance().load(activity, item.videoCover, ivImage);
                    String headUrl = "";
                    String praise = "赞";
                    String name = "";
                    if (item.obj != null && item.obj.user != null) {
                        headUrl = item.obj.user.headImage;
                        name = item.obj.user.nickName;
                        praise = item.obj.likeCount + "赞";
                    }
                    ImageLoader.getInstance().loadHead(activity, headUrl, ivHead);
                    tvContent.setText(item.content);
                    tvName.setText(name);
                    tvPraise.setText(praise);
                }
            };
            adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(activity, QuanZiVideoDetailActivity.class);
                    intent.putExtra("data", allList.get(position));
                    jump2Activity(intent);
                }
            });
            recycler.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
    }


    private void loadData() {
        Map<String, Object> map = QuznZiHttp.getMap();
        map.put("containsImage", 0);
        map.put("containsVideo", 1);
        map.put("pageNumber", page);
        map.put("pageSize", pageSize);
        QuznZiHttp.list(map, new BaseEntityOb<PagerModel<ItemQuznZi>>() {
            @Override
            public void onDataDeal(boolean isSuccess, PagerModel<ItemQuznZi> data, String msg) {
                List<ItemQuznZi> list = null;
                if (isSuccess && data != null) {
                    list = data.content;
                }
                if (page == 1) {
                    allList.clear();
                }
                initAdapter(list);
                if (list != null && list.size() >= pageSize) {
                    proxyLayout.setCanLoadMore(true);
                } else {
                    proxyLayout.setCanLoadMore(false);
                }
                if (allList.isEmpty()) {
                    proxyLayout.showEmptyView();
                } else {
                    proxyLayout.showContentView();
                }
                proxyLayout.dragFinish();
            }
        });
    }
}